package com.blockmeta.bbs.businesslibrary.umeng.circle;

import android.app.Activity;
import android.graphics.Bitmap;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blockmeta.bbs.baselibrary.utils.ImageTools;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.umeng.ShareResTask;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;

/**
 * @author AirThor
 * @date 2020/3/31.
 * 圈子分享
 */
public class IMShare {
    private Activity mActivity;

    private IShareObject mShareMedia;

    public IMShare(Activity activity) {
        if (activity == null) {
            throw new NullPointerException("activity must not be null");
        }
        mActivity = new WeakReference<>(activity).get();
    }

    public IMShare withMedia(IShareObject shareMedia){
        mShareMedia = shareMedia;
        return this;
    }

    /**
     * 分享到圈子
     */
    public void share(){
        if (mShareMedia == null){
            throw new NullPointerException("Share data must not be null");
        }
        ShareResTask.doTask(new ShareResTask.Task<ShareContent>() {
            @Override
            public ShareContent doInBackground() {
                ShareContent content = assembleShareData(mShareMedia);
                if (mActivity == null){
                    return null;
                }
                return content;
            }

            @Override
            public void doOnUIThread(ShareContent content) {
                if (content == null){
                    ToastUtil.show("分享失败");
                    return;
                }
                ARouter.getInstance().build(RouterPath.IM_SHARE_ACTIVITY)
                        .withParcelable(Constant.ZERO,content)
                        .navigation(mActivity);
            }
        });
    }

    /**
     *  根据传入的媒体对象组装分享需要的序列化数据
     * @param shareMedia 媒体对象
     * @return 序列化数据
     */
    private ShareContent assembleShareData(IShareObject shareMedia) {
        IShareObject.ShareType i = shareMedia.getShareType();
        if (i == IShareObject.ShareType.WEB_PAGE) {
            return assembleWebPageData((IMWeb) shareMedia);
        } else if (i == IShareObject.ShareType.IMAGE) {
            return assembleImageData((IMImage) shareMedia);
        }else if (i == IShareObject.ShareType.CUSTOM){
            return assembleCustomData((IMCustom)shareMedia);
        }else {
            return null;
        }
    }

    private ShareContent assembleCustomData(IMCustom shareMedia) {
        ShareContent content = new ShareContent();
        content.mCustomShareJson = shareMedia.getCustomShareJson();
        content.mShareType = shareMedia.getShareType();
        return content;
    }

    private ShareContent assembleImageData(IMImage shareMedia) {
        ShareContent content = new ShareContent();
        Bitmap bitmap = shareMedia.getBitmap();
        //判断图片要压缩保存的目录是不是存在,不存在就创建
        content.mBitmap = saveBitmapToFile(bitmap);
        content.mShareType = shareMedia.getShareType();
        return content;
    }

    private ShareContent assembleWebPageData(IMWeb shareMedia) {
        ShareContent content = new ShareContent();
        content.mTitle = shareMedia.getTitle();
        content.mDescribe = shareMedia.getDescribe();
        content.mThumb = shareMedia.getThumb();
        content.mUrl = shareMedia.getUrl();
        content.mTypeDescribe = shareMedia.getTypeDescribe();
        content.mShareType = shareMedia.getShareType();
        return content;
    }

    public String saveBitmapToFile(Bitmap bitmap) {
        //判断图片要压缩保存的目录是不是存在,不存在就创建
        File dir = new File(ImageTools.app_file_dir, "captured_photos");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir,"share.jpg");
        if (file.exists()){
            file.delete();
        }
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            bitmap.recycle();
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
